package com.gx.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gx.model.pojo.*;
import com.gx.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 流程数据封装
 *
 * @author lpj
 */
@Service
@Slf4j
public class FlowInfoServiceImpl implements FlowInfoService {

    @Resource
    private TbFlowDataService flowDataService;

    @Resource
    private TbFlowStartService flowStartService;

    @Resource
    private TbFlowEndService flowEndService;

    @Resource
    private TbBusinessTaskService taskBusinessService;

    @Resource
    private TbFlowClaimService claimService;

    @Override
    public void saveClaim(TbFlowClaim flowClaim) {
        boolean result = claimService.save(flowClaim);
        log.info("保存签收流程数据{}:{}", result, flowClaim);
    }

    @Override
    public void saveFlowData(TbFlowData flowData) {
        boolean result = this.flowDataService.save(flowData);
        log.info("保存执行流程数据{}:{}", result, flowData);
    }

    @Override
    public void saveFlowStart(TbFlowStart flowStart) {
        boolean result = this.flowStartService.save(flowStart);
        log.info("保存启动流程数据{}:{}", result, flowStart);
    }

    @Override
    public void updateFlowStart(String taskFomId, String nextId, String nextName, Map<String, Object> processVariables) {
        LambdaQueryWrapper<TbFlowStart> flowStartWrapper = new LambdaQueryWrapper<>();
        flowStartWrapper.eq(TbFlowStart::getTaskFormId, taskFomId);
        TbFlowStart flowStart = this.flowStartService.getOne(flowStartWrapper);
        if (flowStart == null) {
            return;
        }
        flowStart.setNextTaskId(nextId);
        flowStart.setNextTaskName(nextName);
        boolean result = this.flowStartService.updateById(flowStart);
        log.info("修改启动流程数据{}:{}", result, flowStart);
    }

    @Override
    public void saveFlowEnd(TbFlowEnd flowEnd) {
        boolean result = this.flowEndService.save(flowEnd);
        log.info("保存结束流程数据{}:{}", result, flowEnd);
    }

    @Override
    public void delStartFlow(String taskFormId) {
        LambdaQueryWrapper<TbFlowStart> flowStartWrapper = new LambdaQueryWrapper<>();
        flowStartWrapper.eq(TbFlowStart::getTaskFormId, taskFormId);
        boolean result = this.flowStartService.remove(flowStartWrapper);
        log.info("删除启动流程数据{}:{}", result, taskFormId);
    }

    @Override
    public void delTaskFormFlow(List<String> taskFormIds) {
        if (CollUtil.isNotEmpty(taskFormIds)) {
            LambdaQueryWrapper<TbFlowStart> flowStartWrapper = new LambdaQueryWrapper<>();
            flowStartWrapper.in(TbFlowStart::getTaskFormId, taskFormIds);
            this.flowStartService.remove(flowStartWrapper);

            LambdaQueryWrapper<TbFlowData> flowDataWrapper = new LambdaQueryWrapper<>();
            flowDataWrapper.in(TbFlowData::getTaskFormId, taskFormIds);
            this.flowDataService.remove(flowDataWrapper);

            LambdaQueryWrapper<TbFlowEnd> flowEndWrapper = new LambdaQueryWrapper<>();
            flowEndWrapper.in(TbFlowEnd::getTaskFormId, taskFormIds);
            this.flowEndService.remove(flowEndWrapper);

            LambdaQueryWrapper<TbBusinessTask> taskFormWrapper = new LambdaQueryWrapper<>();
            taskFormWrapper.in(TbBusinessTask::getId, taskFormIds);
            this.taskBusinessService.remove(taskFormWrapper);

            LambdaQueryWrapper<TbFlowClaim> claimWrapper = new LambdaQueryWrapper<>();
            claimWrapper.in(TbFlowClaim::getTaskFormId, taskFormIds);
            this.claimService.remove(claimWrapper);
            log.info("级联删除流程数据:{}", taskFormIds);
        }
    }
}
